package cn.com.elite;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import dnapay.common.DateFormatter;
import dnapay.common.Strings;
import dnapay.common.ToolKit;
import dnapay.service.client.PosMessage;
import dnapay.service.client.TransactionClient;
import dnapay.service.client.TransactionType;

public class HTPayService extends HttpServlet{
	private static final Log log=LogFactory.getLog(HTPayService.class);
	public static String WSDL="";
	public static String Key="";
	public static String URL="";
	public static String MerchantNo="";
	public static String MerchantType="";
	public static String MerchantPassWD="";
	public static String TerminalNo="";
	public static String jiaoyi_shenqing="";
	public static String jiaoyi_chexiao="";
	public static String jiaoyi_chenggong="";
	public static String USER="";
	public static String PASSWORD="";
	public static cn.com.elite.HTBean ebean;
	private static String Remark="";
	
	public static String getERRO(Exception e){
		String str="";
		try{
            StringWriter sw=new StringWriter();  
            e.printStackTrace(new PrintWriter(sw,true));  
            str=sw.toString(); 	
		}catch(Exception e1){
			str=e.toString();
			log.error("getERRO:"+e1.toString());
		}
		return str;
	}
	
	public HTBean getbean(){
		ServletContext application;   
        WebApplicationContext wac;   
        application = getServletContext();   
        wac=WebApplicationContextUtils.getWebApplicationContext(application);//获取spring的context  
        cn.com.elite.HTBean ebean=(cn.com.elite.HTBean)wac.getBean("ebean");  
        return ebean;
	} 
	
	public void init(){
		try{
			ebean=getbean();			
			WSDL=this.getInitParameter("WSDL");
			log.info("WSDL:"+WSDL);
			Key=this.getInitParameter("Key");
			log.info("Key:"+Key);
			URL=this.getInitParameter("URL");
			log.info("URL:"+URL);
			MerchantType=this.getInitParameter("MerchantType");
			log.info("MerchantType:"+MerchantType);
			MerchantNo=this.getInitParameter("MerchantNo");
			log.info("MerchantNo:"+MerchantNo);
			MerchantPassWD=this.getInitParameter("MerchantPassWD");
			log.info("MerchantPassWD:"+MerchantPassWD);
			TerminalNo=this.getInitParameter("TerminalNo");
			log.info("MerchantNo:"+TerminalNo);
			jiaoyi_shenqing=this.getInitParameter("jiaoyi_shenqing");
			log.info("jiaoyi_shenqing:"+jiaoyi_shenqing);
			jiaoyi_chexiao=this.getInitParameter("jiaoyi_chexiao");
			log.info("jiaoyi_chexiao:"+jiaoyi_chexiao);
			jiaoyi_chenggong=this.getInitParameter("jiaoyi_chenggong");
			log.info("jiaoyi_chenggong:"+jiaoyi_chenggong);
			USER=this.getInitParameter("USER");
			log.info("USER:"+USER);
			PASSWORD=this.getInitParameter("PASSWORD");
			log.info("PASSWORD:"+PASSWORD);
			
			String sql1=this.getInitParameter("sql1");
			log.info("sql1:"+sql1);
			String sql2=this.getInitParameter("sql2");
			log.info("sql2:"+sql2);
			String sql3=this.getInitParameter("sql3");
			log.info("sql3:"+sql3);
			String sql4=this.getInitParameter("sql4");
			log.info("sql4:"+sql4);
			String HZ=this.getInitParameter("HZ");
			log.info("HZ:"+HZ);
			HTPayThread td=new HTPayThread();
			td.setChaxun_sql1(sql1);
			td.setChaxun_sql2(sql2);
			td.setChaxun_sql3(sql3);
			td.setChaxun_sql4(sql4);
			td.setHZ(Integer.parseInt(HZ));
			Thread t=new Thread(td); 
	        t.start(); //   启动任务
		}catch(Exception e){
			log.error("init():"+getERRO(e));
		}
	}
	public static String getJyCxResult(String xml){
		String responseStr="";
		try{
			HttpClient httpclient=new DefaultHttpClient();
			HttpPost httppost=new HttpPost(jiaoyi_chexiao);
			StringEntity entity=new StringEntity(xml,"GBK");
			httppost.setEntity(entity);
			httppost.setHeader("Content-type", "text/xml;charSet=GBK");
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			Date startDate = new Date();
			log.info("调用中台交易撤销接口开始时间："+startDate.toString());
			responseStr=httpclient.execute(httppost,responseHandler);
			Date endDate=new Date();
			log.info("调用中台交易撤销接口结束时间："+endDate.toString());
			long between=endDate.getTime()-startDate.getTime();
			log.info("共用毫秒数："+between);
			log.info("交易撤销返回XML"+responseStr);
			
			Document doc=DocumentHelper.parseText(responseStr); 
			Element root=doc.getRootElement(); 
			Element HEAD=root.element("HEAD"); 
			Element BODY=root.element("BODY");
			Element TRANS_CODE_Element=HEAD.element("TRANS_CODE");
			String TRANS_CODE=TRANS_CODE_Element.getText();
			log.info("TRANS_CODE:"+TRANS_CODE);
			
			Element ERROR_CODE_Element=HEAD.element("ERROR_CODE");
			String ERROR_CODE=ERROR_CODE_Element.getText();
			log.info("ERROR_CODE:"+ERROR_CODE);		
			Element TRANS_MESSAGE_Element=HEAD.element("TRANS_MESSAGE");
			String TRANS_MESSAGE=TRANS_MESSAGE_Element.getText();
			log.info("TRANS_MESSAGE:"+TRANS_MESSAGE);	
			Element CANCELINFO_DTO_Element=BODY.element("CANCELINFO_DTO");
			Element POA_CODE_Element=CANCELINFO_DTO_Element.element("POA_CODE");
			String POA_CODE=POA_CODE_Element.getText();
			log.info("POA_CODE:"+POA_CODE);
			Element OPERATER_CODE_Element=CANCELINFO_DTO_Element.element("OPERATER_CODE");
			String OPERATER_CODE=OPERATER_CODE_Element.getText();
			log.info("OPERATER_CODE:"+OPERATER_CODE);
			Element CANCEL_DTO_LIST_Element=CANCELINFO_DTO_Element.element("CANCEL_DTO_LIST");
			Iterator iters=CANCEL_DTO_LIST_Element.elementIterator();
			while(iters.hasNext()){
				Element CANCEL_DTO=(Element)iters.next();
				Element BUSINESS_NO_Element=CANCEL_DTO.element("BUSINESS_NO");
				String BUSINESS_NO=BUSINESS_NO_Element.getText();
				log.info("BUSINESS_NO:"+BUSINESS_NO);
				try{
					String HTPay_3_result=ebean.HTPay_3(POA_CODE,OPERATER_CODE,BUSINESS_NO,TRANS_CODE,ERROR_CODE,TRANS_MESSAGE);
					log.info("HTPay_3_result:"+HTPay_3_result);
				}catch(Exception e){
					log.error(getERRO(e));
				}
			}
		}catch(Exception e){
			log.info("getJyCxResult:"+getERRO(e));
		}
		return responseStr;
	}
	public static String getJyCgResult(String xml){
		String responseStr="";
		try{
			HttpClient httpclient=new DefaultHttpClient();
			HttpPost httppost=new HttpPost(jiaoyi_chenggong);
			StringEntity entity=new StringEntity(xml,"GBK");
			httppost.setEntity(entity);
			httppost.setHeader("Content-type", "text/xml;charSet=GBK");
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			Date startDate = new Date();
			log.info("调用中台交易成功接口开始时间："+startDate.toString());
			responseStr=httpclient.execute(httppost,responseHandler);
			Date endDate=new Date();
			log.info("调用中台交易成功接口结束时间："+endDate.toString());
			long between=endDate.getTime()-startDate.getTime();
			log.info("共用毫秒数："+between);
			log.info("交易成功返回XML"+responseStr);
			
			Document doc=DocumentHelper.parseText(responseStr); 
			Element root=doc.getRootElement(); 
			Element HEAD=root.element("HEAD"); 
			Element BODY=root.element("BODY");
			Element TRANS_CODE_Element=HEAD.element("TRANS_CODE");
			String TRANS_CODE=TRANS_CODE_Element.getText();
			log.info("TRANS_CODE:"+TRANS_CODE);
			
			Element TRANS_MESSAGE_Element=HEAD.element("TRANS_MESSAGE");
			String TRANS_MESSAGE=TRANS_MESSAGE_Element.getText();
			log.info("TRANS_MESSAGE:"+TRANS_MESSAGE);
			Element ERROR_CODE_Element=HEAD.element("ERROR_CODE");
			String ERROR_CODE=ERROR_CODE_Element.getText();
			log.info("ERROR_CODE:"+ERROR_CODE);		
			
			Element PAY_INFO_DTO_Element=BODY.element("PAY_INFO_DTO");
			Element POA_CODE_Element=PAY_INFO_DTO_Element.element("POA_CODE");
			String POA_CODE=POA_CODE_Element.getText();
			log.info("POA_CODE:"+POA_CODE);
			Element TRANS_SERIAL_NO_Element=PAY_INFO_DTO_Element.element("TRANS_SERIAL_NO");
			String TRANS_SERIAL_NO=TRANS_SERIAL_NO_Element.getText();
			log.info("TRANS_SERIAL_NO:"+TRANS_SERIAL_NO);
			Element PAY_DETAILE_DTO_LIST_Element=PAY_INFO_DTO_Element.element("PAY_DETAILE_DTO_LIST");
			Iterator iters=PAY_DETAILE_DTO_LIST_Element.elementIterator();
			while(iters.hasNext()){
				Element PAY_DETAILE_DTO=(Element)iters.next();
				Element BUSINESS_NO_Element=PAY_DETAILE_DTO.element("BUSINESS_NO");
				String BUSINESS_NO=BUSINESS_NO_Element.getText();
				log.info("BUSINESS_NO:"+BUSINESS_NO);
				Element POLICY_NO_Element=PAY_DETAILE_DTO.element("POLICY_NO");
				String POLICY_NO=POLICY_NO_Element.getText();
				log.info("POLICY_NO:"+POLICY_NO);
				Element POLICY_STATUS_Element=PAY_DETAILE_DTO.element("POLICY_STATUS");
				String POLICY_STATUS=POLICY_STATUS_Element.getText();
				log.info("POLICY_STATUS:"+POLICY_STATUS);
				Element FAIL_REASON_CODE_Element=PAY_DETAILE_DTO.element("FAIL_REASON_CODE");
				String FAIL_REASON_CODE=FAIL_REASON_CODE_Element.getText();
				log.info("FAIL_REASON_CODE:"+FAIL_REASON_CODE);
				Element FAIL_REASON_Element=PAY_DETAILE_DTO.element("FAIL_REASON");
				String FAIL_REASON=FAIL_REASON_Element.getText();
				log.info("FAIL_REASON:"+FAIL_REASON);
				try{
					String HTPay_2_result=ebean.HTPay_2(POA_CODE,TRANS_SERIAL_NO,BUSINESS_NO,POLICY_NO,POLICY_STATUS,FAIL_REASON_CODE,FAIL_REASON,TRANS_CODE,TRANS_MESSAGE,ERROR_CODE);
					log.info("HTPay_2_result:"+HTPay_2_result);	
				}catch(Exception e){
					log.error(getERRO(e));
				}

			}
		}catch(Exception e){
			log.info("getJyCxResult:"+getERRO(e));
		}
		return responseStr;
	}
	private String getJySqResult(String LOG_GUID,String xml){
		String merOrderNo="";
		String responseStr="";
		try{
			HttpClient httpclient=new DefaultHttpClient();
			HttpPost httppost=new HttpPost(jiaoyi_shenqing);
			StringEntity entity=new StringEntity(xml,"GBK");
			httppost.setEntity(entity);
			httppost.setHeader("Content-type", "text/xml;charSet=GBK");
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			Date startDate = new Date();
			log.info("调用中台交易申请接口开始时间："+startDate.toString());
			responseStr=httpclient.execute(httppost,responseHandler);
			Date endDate=new Date();
			log.info("调用中台交易申请接口结束时间："+endDate.toString());
			long between=endDate.getTime()-startDate.getTime();
			log.info("共用毫秒数："+between);
			log.info("交易申请返回XML"+responseStr);		
			Document doc=DocumentHelper.parseText(responseStr); 
			Element root=doc.getRootElement(); 
			Element HEAD=root.element("HEAD"); 
			Element BODY=root.element("BODY"); 
			Element RESPONSE_CODE_Element=HEAD.element("RESPONSE_CODE");
			Element TRANS_CODE_Element=HEAD.element("TRANS_CODE");
			String RESPONSE_CODE=RESPONSE_CODE_Element.getText();
			log.info("RESPONSE_CODE:"+RESPONSE_CODE);
			String TRANS_CODE=TRANS_CODE_Element.getText();
			if(RESPONSE_CODE.equals("1")){
				Element BILL_DTO=BODY.element("BILL_DTO"); 
				Element POA_CODE_Element=BILL_DTO.element("POA_CODE");
				Element TOTAL_AMOUNT_Element=BILL_DTO.element("TOTAL_AMOUNT");
				Element ITEM_DTO_LIST=BILL_DTO.element("ITEM_DTO_LIST");		
				String POA_CODE=POA_CODE_Element.getText();
				String TOTAL_AMOUNT=TOTAL_AMOUNT_Element.getText();
				log.info("POA_CODE:"+POA_CODE);	
				merOrderNo=POA_CODE;
				log.info("merOrderNo加‘02’之前:"+merOrderNo);
				try{
					String HG2=ebean.HTPay_log(LOG_GUID,"02"+merOrderNo,responseStr,"2");
					log.info("HG2:"+HG2);
				}catch(Exception e){
					log.error(getERRO(e));
				}
				Iterator iters=ITEM_DTO_LIST.elementIterator();
				while(iters.hasNext()){
					Element ITEM_DTO=(Element)iters.next();
					Element BUSINESS_NO_Element=ITEM_DTO.element("BUSINESS_NO");
					String BUSINESS_NO=BUSINESS_NO_Element.getText();
					log.info("BUSINESS_NO:"+BUSINESS_NO);
					Element PREMIUM_Element=ITEM_DTO.element("PREMIUM");
					String PREMIUM=PREMIUM_Element.getText();
					log.info("PREMIUM:"+PREMIUM);
					Element TAXFEE_Element=ITEM_DTO.element("TAXFEE");
					String TAXFEE=TAXFEE_Element.getText();
					log.info("TAXFEE:"+PREMIUM);
					Element AMOUNT_Element=ITEM_DTO.element("AMOUNT");
					String AMOUNT=AMOUNT_Element.getText();
					log.info("AMOUNT:"+PREMIUM);
					try{
						String HTPay_1_result=ebean.HTPay_1(POA_CODE,TOTAL_AMOUNT,BUSINESS_NO,PREMIUM,TAXFEE,AMOUNT,TRANS_CODE);
						log.info("HTPay_1_result:"+HTPay_1_result);
					}catch(Exception e){
						log.error(getERRO(e));
					}
				}
			}else{
				String HG2=ebean.HTPay_log(LOG_GUID,"",responseStr,"2");
				log.info("HG2:"+HG2);
				return "";
			}			
		}catch(Exception e){
			log.error("getJySqResult(String xml):"+getERRO(e));
			String HG2=ebean.HTPay_log(LOG_GUID,"",responseStr,"2");
			log.info("HG2:"+HG2);
			return "";
		}
		return merOrderNo;
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException{
		TransactionClient tm=new TransactionClient(WSDL,"");
	    tm.setTransactionType(TransactionType.CA);
		tm.setServerCert(ToolKit.getPropertyFromFile(Key)); 
		String encryptKey=Strings.random(24);
		String returnUrl=URL;
		System.setProperty("javax.net.ssl.trustStore", ToolKit.getPropertyFromFile("javax.net.ssl.trustStore"));
	    System.setProperty("javax.net.ssl.trustStorePassword", ToolKit.getPropertyFromFile("javax.net.ssl.trustStorePassword"));
	    tm.setMerchantNo(MerchantType+MerchantNo);
	    tm.setMerchantPassWD(MerchantPassWD);
	    tm.setTerminalNo(TerminalNo);
	    request.setCharacterEncoding("UTF-8");
	    boolean payNow=true;
	    String pin="";
	    String mobileNumber=request.getParameter("mobileNumber");
	    log.info("mobileNumber:"+mobileNumber);
	    String bankCardNo=request.getParameter("bankCardNo");
	    log.info("bankCardNo:"+bankCardNo);
	    String accountNum="14"+mobileNumber+"|"+bankCardNo;
	    String idCardType = request.getParameter("idCardType");                   
	    log.info("idCardType:"+idCardType);
	    String idCardNo = request.getParameter("idCardNo");    
	    log.info("idCardNo:"+idCardNo);
	    String idCardName = request.getParameter("idCardName");    
	    idCardName=URLDecoder.decode(idCardName,"UTF-8");
	    log.info("idCardName:"+idCardName);
	    String userName = request.getParameter("userName");    
	    userName=URLDecoder.decode(userName,"UTF-8");
	    log.info("userName:"+userName);
	    String bankAddress = request.getParameter("bankAddress");        
	    bankAddress=URLDecoder.decode(bankAddress,"UTF-8");
	    log.info("bankAddress:"+bankAddress);
	    String ipAddress = request.getParameter("ipAddress");        
	    log.info("ipAddress:"+ipAddress);
	    String idCardAddress = request.getParameter("idCardAddress");                 
	    log.info("idCardAddress:"+idCardAddress);
	    String productPhoneNumber = request.getParameter("productPhoneNumber");           
	    log.info("productPhoneNumber:"+productPhoneNumber);
	    String productAddress = request.getParameter("productAddress");               
	    log.info("productAddress:"+productAddress);
	    String bankPhoneNumber = request.getParameter("bankPhoneNumber");
	    log.info("bankPhoneNumber:"+bankPhoneNumber);
	    String extTransData = request.getParameter("extTransData");                  
	    log.info("extTransData :"+extTransData );	  
	    String orderAmount = request.getParameter("orderAmount");
	    log.info("orderAmount :"+orderAmount );
	    String orderDescription = request.getParameter("orderDescription");
	    log.info("orderDescription :"+orderDescription );
	    String orderRemark = request.getParameter("orderRemark");
	    log.info("orderRemark :"+orderRemark );
	    String dabao_body = request.getParameter("dabao_body");
	    log.info("dabao_body:"+dabao_body);
	    String COM_CODE = request.getParameter("COM_CODE");
	    log.info("COM_CODE:"+COM_CODE);
	    String LOG_GUID = request.getParameter("LOG_GUID");
	    log.info("LOG_GUID:"+LOG_GUID);
	    String type = request.getParameter("type");
	    log.info("type:"+type);
	    String merOrderNo="";
	    String JySQXml="<PACKET type='REQUEST' version='1.0'><HEAD><REQUEST_TYPE>62</REQUEST_TYPE><COM_CODE>"+COM_CODE+"</COM_CODE><USER>"+USER+"</USER><PASSWORD>"+PASSWORD+"</PASSWORD><INTERIONAL_STANDARD_CODE/></HEAD>" +dabao_body+"</PACKET>";
	    log.info("JySQXml:"+JySQXml);
	    String HG1=ebean.HTPay_log(LOG_GUID,"",JySQXml,"1");	   
	    log.info("HG1:"+HG1);  
	    String merOrderNo2=getJySqResult(LOG_GUID,JySQXml); 
	    log.info("merOrderNo2:"+merOrderNo2);
	    merOrderNo=merOrderNo2+getMerchantOrderNO()+COM_CODE;
	     
//	    String merOrderNo=getMerchantOrderNO(); 
	    try{	    	
	       if(merOrderNo2.length()>2){
	    	   merOrderNo="12"+merOrderNo;
	    	   log.info("merOrderNo:"+merOrderNo);	    	   
	    	   String transDataFirstNewUser="|" + "|" + "|" + "|"+ "|"+ "|"+ "|"+ "|"+ "|";
	    	   log.info("transDataFirstNewUser:"+transDataFirstNewUser);
	    	   String transDataNewUser=idCardName + "|" + idCardNo + "|" + bankAddress + "|" + idCardType + "|"+ userName + "|" + ipAddress + "|" + idCardAddress + "|"+ productPhoneNumber + "|" + productAddress + "|" + bankPhoneNumber + "|" + extTransData;
	    	   log.info("transDataNewUser:"+transDataNewUser);
	    	   String transDataOldUser ="|" + "|" + "|" + "|"+ userName + "|" + ipAddress + "|" + idCardAddress + "|"+ productPhoneNumber + "|" + productAddress + "|" + bankPhoneNumber + "|" + extTransData;
	    	   log.info("transDataOldUser:"+transDataOldUser);
	    	   String transDataNewUser1=idCardName + "|"  + "|" +  "|" +  "|"+ userName + "|" +  "|" + "|"+  "|" +  "|" +  "|" ;
	    	   log.info("transDataNewUser1:"+transDataNewUser1);
	    	   PosMessage pm=null;
	    	   String yanzheng="";
	    	   String money="";
	    	   try{
	    		   String HG3=ebean.HTPay_log(LOG_GUID,merOrderNo,transDataFirstNewUser,"3");
		    	   log.info("HG3:"+HG3); 
	    	   }catch(Exception e){
	    		   log.error(getERRO(e));
	    	   }
	    	   log.info("accountNum:"+accountNum);
	    	   pm=tm.accountQuery(getSerialNO(),accountNum,transDataFirstNewUser,encryptKey);
	    	   try{
	    		   String HG4=ebean.HTPay_log(LOG_GUID,merOrderNo,pm.toString(),"4");
		    	   log.info("HG4:"+HG4);
	    	   }catch(Exception e){
	    		   log.error(getERRO(e));
	    	   }
	    	   log.info("pm:"+pm);
	    	   yanzheng=pm.getRespCode();
	    	   log.info("yanzheng:"+yanzheng);
	    	   money=pm.getAmount();
	    	   log.info("money:"+money);
	    	   if(Float.parseFloat(orderAmount)>Float.parseFloat(money)){
	    		   getJyCxProcess(merOrderNo,LOG_GUID,COM_CODE,USER,PASSWORD);
		           return;
		        }else if(pm.getRespCode().equals("0000")){
		        	String zhifu_result="";
			        try{
			        	zhifu_result=YlZhifu(LOG_GUID,merOrderNo,transDataNewUser1,accountNum,pin,orderAmount,orderDescription,orderRemark,payNow,returnUrl,encryptKey,tm,pm);	
			        }catch(Exception e){
			        	getJyCxProcess(merOrderNo,LOG_GUID,COM_CODE,USER,PASSWORD);
			        	log.error("YlZhifu_error:"+getERRO(e));
			        	return;
			        }  		       
			       try{
			    	   if(zhifu_result.equals("0000")){
			        	   getJyCgProcess(merOrderNo,LOG_GUID,COM_CODE,USER,PASSWORD);
			           }else if(zhifu_result.equals("00A3")){
			           }else{
			        	   getJyCxProcess(merOrderNo,LOG_GUID,COM_CODE,USER,PASSWORD);
			           }
			       }catch(Exception e){
			    	   log.error(getERRO(e));
			       }
		        }else if(pm.getRespCode().equals("T437")||pm.getRespCode().equals("T438")){
		        	String zhifu_result="";
			        try{
			        	zhifu_result=YlZhifu(LOG_GUID,merOrderNo,transDataNewUser,accountNum,pin,orderAmount,orderDescription,orderRemark,payNow,returnUrl,encryptKey,tm,pm);	
			        }catch(Exception e){
			        	getJyCxProcess(merOrderNo,LOG_GUID,COM_CODE,USER,PASSWORD);
			        	log.error("YlZhifu_error:"+getERRO(e));
			        	return;
			        }  		       
			       try{
			    	   if(zhifu_result.equals("0000")){
			        	   getJyCgProcess(merOrderNo,LOG_GUID,COM_CODE,USER,PASSWORD);
			           }else if(zhifu_result.equals("00A3")){
			           }else{
			        	   getJyCxProcess(merOrderNo,LOG_GUID,COM_CODE,USER,PASSWORD);
			        	   return;
			           }
			       }catch(Exception e){
			    	   log.error(getERRO(e)); 
			       }
		        }else{
		        	getJyCxProcess(merOrderNo,LOG_GUID,COM_CODE,USER,PASSWORD);
		        	return;
		        }
	    	   request.setCharacterEncoding("UTF-8");
		       response.setContentType("text/html;charset=utf-8;");
		       PrintWriter out=response.getWriter();
		       out.print(Remark);
	       }
		}catch(Exception e){
			log.error("pay_error:"+getERRO(e));
			getJyCxProcess(merOrderNo,LOG_GUID,COM_CODE,USER,PASSWORD);
		}
	}
	public static String YlZhifu(String LOG_GUID,String merOrderNo,String transDataNewUser,String accountNum,String pin,String orderAmount,String orderDescription,String orderRemark,boolean payNow,String returnUrl,String encryptKey,TransactionClient tm,PosMessage pm) throws Exception{
		try{
			String HG5=ebean.HTPay_log(LOG_GUID,merOrderNo,transDataNewUser,"5");
		    log.info("HG5:"+HG5);	
		}catch(Exception e){
			log.error(getERRO(e));
		}	
	    log.info("accountNum:"+accountNum); 
	    pm=tm.pay(getSerialNO(),accountNum,pin,orderAmount,merOrderNo, "", orderDescription, orderRemark, payNow, returnUrl,transDataNewUser,encryptKey);		           
        try{
        	String HG6=ebean.HTPay_log(LOG_GUID,merOrderNo,pm.toString(),"6");
    	    log.info("HG6:"+HG6);
        }catch(Exception e){
        	log.error(getERRO(e));
        }
        log.info("pm1:"+pm);
        String zhifu_result=pm.getRespCode();
        Remark=pm.getRemark();
        log.info("pm1:"+zhifu_result);
        log.info("Remark:"+Remark);
        return zhifu_result;
	}
	public static void getJyCgProcess(String merOrderNo,String LOG_GUID,String COM_CODE,String USER1,String PASSWORD1){
		String success_body_xml=ebean.chaxunString(" select HTPay_Func('success','"+merOrderNo+"') from dual");
    	log.info("success_body_xml:"+success_body_xml);
    	String success_xml="<PACKET><HEAD><REQUEST_TYPE>18</REQUEST_TYPE><USER>"+USER1+"</USER>" +"<PASSWORD>"+PASSWORD1+"</PASSWORD>" +"<INTERIONAL_STANDARD_CODE/>" +"<COM_CODE>"+COM_CODE+"</COM_CODE></HEAD>" +success_body_xml+"</PACKET>";
    	log.info("success_xml:"+success_xml);
    	try{
    		String HG7=ebean.HTPay_log(LOG_GUID,merOrderNo,success_xml,"7");
    	    log.info("HG7:"+HG7);
    	}catch(Exception e){
    		log.error(getERRO(e));
    	}
    	String cg_result=getJyCgResult(success_xml);
    	try{
    		String HG8=ebean.HTPay_log(LOG_GUID,merOrderNo,cg_result,"8");
    	    log.info("HG8:"+HG8);
    	}catch(Exception e){
    		log.error(getERRO(e));
    	}
	}
	public static void getJyCxProcess(String merOrderNo,String LOG_GUID,String COM_CODE,String USER1,String PASSWORD1){
		String fail_body_xml=ebean.chaxunString(" select HTPay_Func('fail','"+merOrderNo+"') from dual");
    	log.info("fail_body_xml:"+fail_body_xml);
    	String fail_xml="<?xml version='1.0' encoding='UTF-8'?><PACKET version='1.0' type='REQUEST'><HEAD>" +"<REQUEST_TYPE>65</REQUEST_TYPE><USER>"+USER1+"</USER><PASSWORD>"+PASSWORD1+"</PASSWORD><INTERIONAL_STANDARD_CODE/><COM_CODE>"+COM_CODE+"</COM_CODE></HEAD>" +fail_body_xml+"</PACKET>";
    	log.info("fail_xml:"+fail_xml);
    	try{
    		String HG9=ebean.HTPay_log(LOG_GUID,merOrderNo,fail_xml,"9");
    	    log.info("HG9:"+HG9);
    	}catch(Exception e){
    		log.error(getERRO(e));
    	}
    	String cx_result=getJyCxResult(fail_xml);
    	try{
        	String HG10=ebean.HTPay_log(LOG_GUID,merOrderNo,cx_result,"10");
    	    log.info("HG10:"+HG10); 
    	}catch(Exception e){
    		log.error(getERRO(e));
    	}
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException{
		this.doPost(request, response);
	}

    public static String getMerchantOrderNO() {
        return DateFormatter.yyyyMMddHHmmss(new java.util.Date());
    }
    public static String getSerialNO() {
        return dnapay.common.DateFormatter.HHmmss(new java.util.Date());
    }
}
